﻿using System;
using System.Web;
using System.Web.Mvc;

using SimpleForum.Data.Services;

namespace SimpleForum.Classes.Web
{
    public class SimpleViewEngine : IViewEngine
    {
        // should we explictly define Razor as a view engine?
        public RazorViewEngine ViewEngine()
        {
            RazorViewEngine ViewEngine = new RazorViewEngine();
            string CurrentTheme = String.IsNullOrEmpty(ConfigurationService.Theme) ? "Default" : ConfigurationService.Theme;

            var SharedViews = new string[]
            {
                "~/Plugins/{1}/Views/{0}.cshtml",
                "~/Themes/Default/Views/{1}/{0}.cshtml",
                "~/Themes/Default/Views/Shared/{0}.cshtml",
                "~/Themes/Default/Views/Shared/{1}/{0}.cshtml"
            };

            ViewEngine.PartialViewLocationFormats = new string[]
            {
                "~/Themes/" + CurrentTheme + "/Views/{1}/{0}.cshtml",
                "~/Themes/" + CurrentTheme + "/Views/Shared/{0}.cshtml",
                "~/Themes/" + CurrentTheme + "/Views/Shared/{1}/{0}.cshtml",
                SharedViews[0], SharedViews[1], SharedViews[2], SharedViews[3]
            };

            ViewEngine.ViewLocationFormats = new string[]
            {
                "~/Themes/" + CurrentTheme + "/Views/{1}/{0}.cshtml",
                SharedViews[0], SharedViews[1], SharedViews[2], SharedViews[3]
            };

            ViewEngine.MasterLocationFormats = new string[]
            {
                "~/Themes/" + CurrentTheme + "/Views/{1}/{0}.cshtml",
                "~/Themes/" + CurrentTheme + "/Views/Shared/{1}/{0}.cshtml",
                "~/Themes/" + CurrentTheme + "/Views/Shared/{0}.cshtml",
                SharedViews[0], SharedViews[1], SharedViews[2], SharedViews[3]
            };

            return ViewEngine;
        }

        public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            return ViewEngine().FindPartialView(controllerContext, partialViewName, useCache);
        }

        public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            return ViewEngine().FindView(controllerContext, viewName, masterName, useCache);
        }

        public void ReleaseView(ControllerContext controllerContext, IView view)
        {
            this.ReleaseView(controllerContext, view);
        }
    }
}